package lormi.meipin.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class Pinyin4JUtil {
	
	 private static final Logger logger = Logger.getLogger("devLog");  

	 public static Map<String,String> dictionary = new HashMap<String,String>();  
     
     //加载多音字词典  
     static{ 
         BufferedReader br = null;  
         try {
             File file = new File("src\\lormi\\meipin\\util\\duoyinzi.txt");
             br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));

             String line ;
             while((line=br.readLine())!=null){
                 String[] arr = line.split("#");
                 if(StringUtil.isNotNull(arr[1])){
                     String[] sems = arr[1].split(" ");
                     for (String sem : sems) {

                         if(StringUtil.isNotNull(sem)){
                             dictionary.put(sem , arr[0]);
                         }
                     }
                 }
             }
         } catch (FileNotFoundException | UnsupportedEncodingException e) {
             e.printStackTrace();
         }catch (IOException e){
             e.printStackTrace();
         }finally{
             if(br!=null){
                 try {
                     br.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
   
     } 

     public static String[] cnToPinYin(char chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
         HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();  
         outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
         outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
         outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);  
           
         if(chineseCharacter>=32 && chineseCharacter<=125){    //ASCII >=33 ASCII<=125的直接返回 ,ASCII码表：http://www.asciitable.com/  
             return new String[]{String.valueOf(chineseCharacter)};  
         }  
           
         return PinyinHelper.toHanyuPinyinStringArray(chineseCharacter, outputFormat);  
     } 
     
     public static String cnToPinYinF(String str) throws BadHanyuPinyinOutputFormatCombination{
    	 return cnToPinYin(str).substring(0,1);
     }
       
     /** 
      * 获取汉字拼音的全拼 
      * @param chineseCharacter 
      * @return 
      * @throws BadHanyuPinyinOutputFormatCombination 
      */  
     public static String cnToPinYin(String chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
    	 if(chineseCharacter==null || chineseCharacter.length()<1){  
             return null;  
         }  
           
         char[] chs = chineseCharacter.toCharArray();  
           
         StringBuilder result = new StringBuilder();  
           
         for(int i=0;i<chs.length;i++){  
             String[] arr = cnToPinYin(chs[i]); //获取拼音列表，包括该字的多音字
             if(arr==null){  
                 result.append("");  
             }else if(arr.length==1){  
                 result.append(arr[0]);  
             }else if(arr[0].equals(arr[1])){  
                 result.append(arr[0]);  
             }else{  
                   
                 String prim = chineseCharacter.substring(i, i+1); 
                   
                 String lst = null,rst = null;  
                   
                 if(i<=chineseCharacter.length()-2){  
                     rst = chineseCharacter.substring(i,i+2);  
                 }  
                 if(i>=1 && i+1<=chineseCharacter.length()){  
                     lst = chineseCharacter.substring(i-1,i+1);  
                 }  
                   
                 String answer = null;  
                 for (String py : arr) {  
                       
                     if(py.length()<1){  
                         continue;  
                     }  
                       
                     if((lst!=null && py.equals(dictionary.get(lst))) ||  
                             (rst!=null && py.equals(dictionary.get(rst)))){ 
                         answer = py;  
                         break;  
                     }  
                       
                     if(py.equals(dictionary.get(prim))){  
                         answer = py;  
                     }  
                 }  
                 if(answer!=null){  
                     result.append(answer);  
                 }else{  
                     logger.warn("no answer ch="+chs[i]);  
                 }  
             }  
         }  
           
         return result.toString().toLowerCase();  
     }  
       
     public static String chineseToPinYinS(String chineseCharacter) throws BadHanyuPinyinOutputFormatCombination{  
         if(StringUtil.isNotNull(chineseCharacter)){
             return null;  
         }  
           
         char[] chs = chineseCharacter.toCharArray();  
           
         StringBuilder result = new StringBuilder();  
           
         for(int i=0;i<chs.length;i++){  
             String[] arr = cnToPinYin(chs[i]);  
             if(arr==null){  
                 result.append("");  
             }else if(arr.length==1){  
                 result.append(arr[0].charAt(0));  
             }else if(arr[0].equals(arr[1])){  
                 result.append(arr[0].charAt(0));  
             }else{  
                   
                 String prim = chineseCharacter.substring(i, i+1);  
                   
                 String lst = null,rst = null;  
                   
                 if(i<=chineseCharacter.length()-2){  
                     rst = chineseCharacter.substring(i,i+2);  
                 }  
                 if(i>=1 && i+1<=chineseCharacter.length()){  
                     lst = chineseCharacter.substring(i-1,i+1);  
                 }    
                   
                 String answer = null;  
                 for (String py : arr) {  
                       
                     if(StringUtil.isNotNull(py)){
                         continue;  
                     }  
                       
                     if((lst!=null && py.equals(dictionary.get(lst))) ||  
                             (rst!=null && py.equals(dictionary.get(rst)))){  
                         answer = py;  
                         break;  
                     }  
                       
                     if(py.equals(dictionary.get(prim))){  
                         answer = py;  
                     }  
                 }  
                 if(answer!=null){  
                     result.append(answer.charAt(0));  
                 }else{  
                     logger.warn("no answer ch="+chs[i]);  
                 }  
             }  
         }  
           
         return result.toString().toLowerCase();  
     } 
     
     public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
    	 char s[] = "长春市".toCharArray();
    	 System.out.println(s[0]);// 长
    	 String str[] = cnToPinYin(s[0]);
    	 System.out.println(str[0]+" | "+str[1]);
    	 
	}
	
}
